﻿using System;
using System.Data.Entity;
using System.IO.Compression;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Ecommerce.Models;
using Library;
using Ecommerce.Migrations;
using Library.MVCHelpers;

namespace Ecommerce
{
    public class MvcApplication : HttpApplication
    {
        protected void Application_Start()
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<UserManagerDbContext, Configuration>());
            NOWCulture.BuildDictionary();
            Settings.RegisterSettings();
            SEOHelper.RegisterUrlDictionary();

            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            ModelBinders.Binders.Add(typeof(DateTime?), new NOWDateTimeModelBinder());
        }

        protected void Application_BeginRequest()
        {

        }

        protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
        {
            if (HttpContext.Current != null)
            {
                HttpResponse response = HttpContext.Current.Response;

                if (response.Filter is GZipStream && response.Headers["Content-encoding"] != "gzip")
                    response.AppendHeader("Content-encoding", "gzip");
                else if (response.Filter is DeflateStream && response.Headers["Content-encoding"] != "deflate")
                    response.AppendHeader("Content-encoding", "deflate");
            }
        }
    }
}
